package com.mbigger.xml.jaxb;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class XmlBuilder {

    public static void main(String[] args) {
        XmlBuilder.write();
        // XmlBuilder.read();
    }

    public static void write() {
        CityList cityList = new CityList();
        City city = new City();
        city.setCityId("我是id1");
        city.setCityCode("他是code1");
        city.setCityName("她是name1");
        city.setProvince("它是Hebei");
        ArrayList<City> list = new ArrayList<City>();
        list.add(city);
        cityList.setCityList(list);

        try {
            OutputStream ops = new FileOutputStream(new File("src/main/resources/citylist-1.xml"));
            JAXBContext context = JAXBContext.newInstance(CityList.class);
            Marshaller marshaller = context.createMarshaller();
            // 设置格式化输出
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(cityList, ops);
            ops.close();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void read() throws Exception {
        // 读取XML文件
        Resource resource = new ClassPathResource("citylist.xml");
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";

        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        br.close();
        // XML转为Java对象
        CityList cityList = (CityList) XmlBuilder.xmlStrToObject(CityList.class, buffer.toString());
        int size = cityList.getCityList().size();
        System.err.println("size of city list is: " + size);
    }

    /**
     * 将XML转化为指定的POJO
     * 
     * @param clazz
     * @param xmlStr
     * @return
     * @throws Exception
     */
    public static Object xmlStrToObject(Class<?> clazz, String xmlStr) {
        Object xmlObject = null;
        Reader reader = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();

            reader = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(reader);
            if (null != reader) {
                reader.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xmlObject;
    }
}
